﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExaminationTT.Model;

namespace ConcreteRules
{
    public class RulesUtils
    {
        public static IEnumerable<ISubscription> GetStudentsIntersectingExams(IExam exam1, IExam exam2)
        {
            return exam1.CUnit.Subscriptions.Intersect(exam2.CUnit.Subscriptions, new ExamIntersectComparator());
        }

        private class ExamIntersectComparator : IEqualityComparer<ISubscription>
        {
            #region IEqualityComparer<ISubscription> Members

            public bool Equals(ISubscription x, ISubscription y)
            {
                return x.Student.Equals(y.Student);
            }

            public int GetHashCode(ISubscription obj)
            {
                return this.GetHashCode();
            }

            #endregion
        }

        
    }

    public class StudentsEnumerable : IEnumerable<IStudent>
    {
        private IEnumerable<ISubscription> _subs;
        private StudentEnumerator _enumerator;
        
        public StudentsEnumerable(IEnumerable<ISubscription> subs)
        {
            _subs = subs;
        }
        #region IEnumerable<IStudent> Members

        public IEnumerator<IStudent> GetEnumerator()
        {
            if (_enumerator == null)
            {
                _enumerator = new StudentEnumerator(_subs.GetEnumerator());
            }
            return _enumerator;
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            if (_enumerator == null)
            {
                _enumerator = new StudentEnumerator(_subs.GetEnumerator());
            }
            return _enumerator;
        }

        #endregion
        
        
        private class StudentEnumerator : IEnumerator<IStudent>
        {
            private IEnumerator<ISubscription> _subs;
            public StudentEnumerator(IEnumerator<ISubscription> subs)
            {
                _subs = subs;
            }
            #region IEnumerator<IStudent> Members

            public IStudent Current
            {
                get { return _subs.Current.Student; }
            }

            #endregion

            #region IDisposable Members

            public void Dispose()
            {
                _subs.Dispose();
            }

            #endregion

            #region IEnumerator Members

            object System.Collections.IEnumerator.Current
            {
                get { return _subs.Current.Student; }
            }

            public bool MoveNext()
            {
                return _subs.MoveNext();
            }

            public void Reset()
            {
                _subs.Reset();
            }

            #endregion
        }
        
    }

    
}
